home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / msysjour / vol07 / 05 / 32gdi / paths.c < prev    next >
C/C++ Source or Header  |  1992-09-01  |  5KB  |  150 lines

  1. /*--------------------------------------
  2.    PATHS.C -- Win32 Paths Demo
  3.               (c) Charles Petzold, 1992
  4.   --------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <string.h>
  8.  
  9. LONG APIENTRY WndProc (HWND, UINT, DWORD, LONG) ;
  10.  
  11. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  12.                     LPSTR lpszCmdParam, int nCmdShow)
  13.      {
  14.      static char szAppName[] = "Paths" ;
  15.      HWND        hwnd ;
  16.      MSG         msg ;
  17.      WNDCLASS    wndclass ;
  18.  
  19.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  20.      wndclass.lpfnWndProc   = WndProc ;
  21.      wndclass.cbClsExtra    = 0 ;
  22.      wndclass.cbWndExtra    = 0 ;
  23.      wndclass.hInstance     = hInstance ;
  24.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  25.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  26.      wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  27.      wndclass.lpszMenuName  = NULL ;
  28.      wndclass.lpszClassName = szAppName ;
  29.  
  30.      RegisterClass (&wndclass) ;
  31.  
  32.      hwnd = CreateWindow (szAppName, "Paths Demo",
  33.                           WS_OVERLAPPEDWINDOW,
  34.                           CW_USEDEFAULT, CW_USEDEFAULT,
  35.                           CW_USEDEFAULT, CW_USEDEFAULT,
  36.                           NULL, NULL, hInstance, NULL) ;
  37.  
  38.      ShowWindow (hwnd, nCmdShow) ;
  39.      UpdateWindow (hwnd) ;
  40.  
  41.      while (GetMessage (&msg, NULL, 0, 0))
  42.           {
  43.           TranslateMessage (&msg) ;
  44.           DispatchMessage (&msg) ;
  45.           }
  46.      return msg.wParam ;
  47.      }
  48.  
  49. LONG APIENTRY WndProc (HWND hwnd, UINT message, DWORD wParam, LONG lParam)
  50.      {
  51.      static char * szOper  [] = { "Stroke, ", "Fill, ", "S & F, " } ;
  52.      static char * szFill  [] = { "Alt", "Wind" } ;
  53.      static char * szWiden [] = { "", " (Widened)" } ;
  54.      static short  cxClient, cyClient ;
  55.      HDC           hdc ;
  56.      int           x, y ;
  57.      PAINTSTRUCT   ps ;
  58.  
  59.      switch (message)
  60.           {
  61.           case WM_SIZE:
  62.                cxClient = LOWORD (lParam) ;
  63.                cyClient = HIWORD (lParam) ;
  64.                return 0 ;
  65.  
  66.           case WM_PAINT:
  67.            hdc = BeginPaint (hwnd, &ps) ;
  68.  
  69.                     // Define a logical display area
  70.  
  71.                SetMapMode (hdc, MM_ANISOTROPIC) ;
  72.                SetWindowExtEx (hdc, 100, 100, NULL) ;
  73.                SetViewportExtEx (hdc, cxClient / 4, cyClient / 3, NULL) ;
  74.  
  75.                     // Set text alignment and brush
  76.  
  77.                SetTextAlign (hdc, TA_UPDATECP) ;
  78.                SelectObject (hdc, GetStockObject (LTGRAY_BRUSH)) ;
  79.  
  80.                for (x = 0 ; x < 4 ; x++)
  81.                for (y = 0 ; y < 3 ; y++)
  82.                     {
  83.                          // Set the proper viewport origin
  84.  
  85.                     SetViewportOrgEx (hdc, x * cxClient / 4,
  86.                                            y * cyClient / 3, NULL) ;
  87.  
  88.                          // Display text descriptions
  89.  
  90.                     MoveToEx (hdc, 0, 0, NULL) ;
  91.  
  92.                     TextOut (hdc, 0, 0, szOper [y],
  93.                                 strlen (szOper [y])) ;
  94.  
  95.                     TextOut (hdc, 0, 0, szFill [x % 2],
  96.                                 strlen (szFill [x % 2])) ;
  97.  
  98.                     TextOut (hdc, 0, 0, szWiden [x > 1],
  99.                                 strlen (szWiden [x > 1])) ;
  100.  
  101.                          // Create a path with an open and closed subpath
  102.  
  103.                     BeginPath (hdc) ;
  104.  
  105.                     MoveToEx (hdc, 10, 30, NULL) ;
  106.                     LineTo   (hdc, 50, 70) ;
  107.                     LineTo   (hdc, 90, 30) ;
  108.  
  109.                     MoveToEx (hdc, 10, 50, NULL) ;
  110.                     LineTo   (hdc, 50, 90) ;
  111.                     LineTo   (hdc, 90, 50) ;
  112.                     CloseFigure (hdc) ;
  113.  
  114.                     EndPath (hdc) ;
  115.  
  116.                          // Possibly widen the path by a 10 width pen
  117.  
  118.                     if (x > 1)
  119.                          {
  120.                          SelectObject (hdc, CreatePen (PS_SOLID, 10, 0)) ;
  121.                          WidenPath (hdc) ;
  122.                          DeleteObject (
  123.                               SelectObject (hdc, GetStockObject (BLACK_PEN))) ;
  124.                          }
  125.  
  126.                          // Set polygon filling mode
  127.  
  128.                     SetPolyFillMode (hdc, x % 2 ? WINDING : ALTERNATE) ;
  129.  
  130.                          // Stroke, fill, or stroke and fill the path
  131.  
  132.                     switch (y)
  133.                          {
  134.                          case 0:   StrokePath (hdc) ;         break ;
  135.                          case 1:   FillPath (hdc) ;           break ;
  136.                          case 2:   StrokeAndFillPath (hdc) ;  break ;
  137.                          }
  138.                     }
  139.  
  140.            EndPaint (hwnd, &ps) ;
  141.                return 0 ;
  142.  
  143.           case WM_DESTROY:
  144.                PostQuitMessage (0) ;
  145.                return 0 ;
  146.           }
  147.  
  148.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  149.      }
  150.